package com.gdth.bank.utils.safeSoftBoard;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

public class SafeEdit extends EditText implements OnDismissListener{

	private SoftKeyBoard softKeyBoard;

	public SafeEdit(Context context) {
		super(context);
		initSafeEdit(context);
	}

	public SafeEdit(Context context, AttributeSet attrs) {
		super(context, attrs);
		initSafeEdit(context);
	}

	public SafeEdit(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initSafeEdit(context);
	}
	
	public void initSafeEdit(Context context){
		//setOnFocusChangeListener(this);
	}
	
	public static void closeKeybord(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (null == imm) {
           return;
        }
        if(imm.isActive()){
            if (null != activity.getCurrentFocus() && null != activity.getCurrentFocus().getWindowToken()) {
                imm.hideSoftInputFromWindow(activity.getCurrentFocus().getApplicationWindowToken(), 0);
            }
        }
    }
	public boolean clearAll = false;
	public boolean isclearAll() {
		return clearAll;
	}

	
	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if(event.getAction()==MotionEvent.ACTION_UP){
			if (softKeyBoard == null) {
				closeKeybord( (Activity) this.getContext());
				softKeyBoard=new SoftKeyBoard(getContext());
				softKeyBoard.isclearAll = isclearAll();
				softKeyBoard.setEdit(this);
				softKeyBoard.setOnDismissListener(this);
				softKeyBoard.show();
				setFocusable(true);
				setFocusableInTouchMode(true);
				requestFocus();
			}
		}
		return true;
	}

	@Override
	public void onDismiss() {
		softKeyBoard.recycle();
		softKeyBoard=null;
	}
}
